﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IBaklogTaskTempService))]
    internal class BaklogTaskTempService : IBaklogTaskTempService
    {
        [Import]
        protected IBaklogTaskTempRepository BaklogTaskTempRepository { get; set; }

        public List<BaklogTaskTempView> GetBaklogTaskTempByNodeCode(string currentFlowNodeCode, string recordID)
        {
            List<BaklogTaskTempView> baklogTaskTempViewList = new List<BaklogTaskTempView>();
            IQueryable<BaklogTaskTemp> baklogTaskTemps = BaklogTaskTempRepository.Entities.Where(b => b.CurrentFlowNodeCode == currentFlowNodeCode && b.RecordID == recordID);
            foreach (BaklogTaskTemp baklogTaskTemp in baklogTaskTemps)
            {
                baklogTaskTempViewList.Add(new BaklogTaskTempView()
                {
                    ID = baklogTaskTemp.ID,
                    RecordID = baklogTaskTemp.RecordID,
                    CurrentFlowNodeCode = baklogTaskTemp.CurrentFlowNodeCode,
                    CurrentProcessPersonDepartment = baklogTaskTemp.CurrentProcessPersonDepartment,
                    CurrentProcessPerson = baklogTaskTemp.CurrentProcessPerson
                });
            }
            return baklogTaskTempViewList;
        }

        public void AddBaklogTaskTemp(List<BaklogTaskTempView> addList)
        {
            foreach (BaklogTaskTempView baklogTaskTempView in addList)
            {
                BaklogTaskTemp existBaklogTaskTemp = BaklogTaskTempRepository.Entities.SingleOrDefault(c => c.ID == baklogTaskTempView.ID);
                if (existBaklogTaskTemp == null)
                 {
                     BaklogTaskTemp baklogTaskTemp = new BaklogTaskTemp()
                     {
                         ID = baklogTaskTempView.ID,
                         RecordID = baklogTaskTempView.RecordID,
                         CurrentFlowNodeCode = baklogTaskTempView.CurrentFlowNodeCode,
                         CurrentProcessPersonDepartment = baklogTaskTempView.CurrentProcessPersonDepartment,
                         CurrentProcessPerson = baklogTaskTempView.CurrentProcessPerson
                     };
                     BaklogTaskTempRepository.Insert(baklogTaskTemp);
                 }
            }
        }

        public void DeleteBaklogTaskTemp(List<BaklogTaskTempView> deleteList)
        {
            foreach (BaklogTaskTempView baklogTaskTempView in deleteList)
            {
                BaklogTaskTempRepository.Delete(baklogTaskTempView.ID);
            }
        }
    }
}